package com.epoint.mobileoa.ui.wsgw.bizlogic;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;

import com.epoint.mobileoa.datanalysis.XMLManager;
import com.epoint.mobileoa.ui.wsgw.model.FormInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.NextStepInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.UserInfoModel;

/**
 * @author liyc
 * @time 2012-12-18 上午10:27:38
 * @annotation 
 */
public class AnalysisForm {

	public static List<FormInfoModel> getFormInfoList(String xml) throws XmlPullParserException, IOException
	{
		XmlPullParser xrp = XMLManager.getInstance();
		StringReader reader = new StringReader(xml);
		xrp.setInput(reader);
		List<FormInfoModel> list = new ArrayList<FormInfoModel>();
		FormInfoModel gm = null;
		while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
			if (xrp.getEventType() == XmlResourceParser.START_TAG) {  
				String name = xrp.getName();  
				if (name.equals("FormField"))
				{
					gm = new FormInfoModel();
					gm.FieldChsName = xrp.getAttributeValue(null, "FieldChsName");
					gm.FieldName = xrp.getAttributeValue(null, "FieldName");
				}
			}
			else if (xrp.getEventType() == XmlPullParser.END_TAG) {
				String name = xrp.getName();  
				if (name.equals("FormField"))
				{
					list.add(gm);
				}  
			}
			else if (xrp.getEventType() == XmlPullParser.TEXT){
				gm.AttributeTextValue = xrp.getText()==null?"":xrp.getText();
			}
			xrp.next();  
		}  
		return list;
	}

	public static List<NextStepInfoModel> getNextStepInfoList(String xml) throws XmlPullParserException, IOException
	{
		XmlPullParser xrp = XMLManager.getInstance();
		StringReader reader = new StringReader(xml);
		xrp.setInput(reader);
		List<NextStepInfoModel> list = new ArrayList<NextStepInfoModel>();
		UserInfoModel u = null;
		NextStepInfoModel gm = null;
		String flag="";
		while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
			if (xrp.getEventType() == XmlResourceParser.START_TAG) {  
				String name = xrp.getName(); 
				if (name.equals("NextStepInfo"))
				{
					gm = new NextStepInfoModel();
				}
				else if(name.equals("StepName"))
				{
					gm.StepName = xrp.nextText();
				}
				else if(name.equals("StepGuid"))
				{
					gm.StepGuid = xrp.nextText();
				}
				else if(name.equals("CanEditUser"))
				{
					gm.CanEditUser = xrp.nextText();
				}
				else if(name.equals("CanEditUser"))
				{
					gm.CanEditUser = xrp.nextText();
				}
				else if(name.equals("FilterHandleUser"))
				{
					gm.filterHandleUserList = new ArrayList<UserInfoModel>();
					flag="filterHandleUserList";
				}
				else if(name.equals("UserInfo"))
				{
					u = new UserInfoModel();
				}
				else if(name.equals("UserGuid"))
				{
					u.UserGuid = xrp.nextText();
				}
				else if(name.equals("UserName"))
				{
					u.UserName = xrp.nextText();
				}
				else if(name.equals("DefaultHandleUser"))
				{
					gm.defaultHandleUserList = new ArrayList<UserInfoModel>();
					flag="defaultHandleUserList";
				}
			}
			else if (xrp.getEventType() == XmlPullParser.END_TAG) {
				String name = xrp.getName();  
				//				if (name.equals("FilterHandleUser"))
				//				{
				//					if(u!=null){
				//						if(u.UserGuid!=null&&u.UserGuid!=null){
				//							gm.filterHandleUserList.add(u);
				//							u=null;
				//						}
				//					}
				//				}  
				//				else 
				if (name.equals("UserInfo"))
				{
					if(u!=null){
						if(u.UserGuid!=null&&u.UserGuid!=null){
							if("defaultHandleUserList".equals(flag)){
								gm.defaultHandleUserList.add(u);
							}else if("filterHandleUserList".equals(flag)){
								gm.filterHandleUserList.add(u);
							}
							u=null;
						}
					}
				}
				else if(name.equals("NextStepInfo"))
				{
					list.add(gm);
				}
			}
			xrp.next();  
		}  
		return list;
	}
}
